package com.shenghui.enterprise.base.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shenghui.enterprise.base.entity.BaseUser;
import com.shenghui.enterprise.base.entity.EntityCommon;
import com.shenghui.enterprise.base.entity.Page;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.utils.MD5;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.entity.PageModel;
import com.shenghui.enterprise.base.mapper.BaseUserMapper;
import com.shenghui.enterprise.base.service.IBaseUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户基本信息表业务逻辑实现类
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Service
@Transactional
public class BaseUserServiceImpl extends ServiceImpl<BaseUserMapper, BaseUser> implements IBaseUserService {
    @Autowired
    private BaseUserMapper userMapper;
    @Autowired
    private EntityCommon entityCommon;

    @Override
    @Transactional
    public Result addBaseUser(BaseUser baseUser, String id) {

        int isOk = 0;
        if (GeneralUtil.isNull(baseUser.getAccount())) {
            return Result.error( "用户名不能为空");
        }
        if (GeneralUtil.isNull(id)) {

            if (GeneralUtil.isNull(baseUser.getPassword())) {
                return Result.error( "密码不能为空");
            }
            // 添加
            entityCommon.createBase(baseUser);
            baseUser.setPassword(MD5.MD5Encode(baseUser.getPassword()));
            isOk = userMapper.insert(baseUser);

        } else {
            // 修改
            entityCommon.modifyBase(baseUser);
            baseUser.setUserId(id);
            if(GeneralUtil.isNotNull(baseUser.getPassword())){
                baseUser.setPassword(MD5.MD5Encode(baseUser.getPassword()));
            }
            isOk = userMapper.updateById(baseUser);
        }
        if (isOk > 0) {
            return Result.ok("保存成功");
        } else {
            return Result.error("保存失败");
        }
    }

    @Override
    public Result getBaseUserById(String userId) {
        // 查询用户
        BaseUser baseUser = userMapper.selectById(userId);
        //baseUser.setPassword(null);
        return Result.ok( "查询成功", baseUser);
    }

    @Override
    public Result deleteBaseUser(String ids) {
        if (GeneralUtil.isNull(ids)) {
            return Result.error("参数【ids】不能为空");
        }
        List<BaseUser> baseUsers = userMapper.selectBatchIds(Arrays.asList(ids));
        for (BaseUser baseUser: baseUsers) {
            userMapper.deleteById(baseUser);
        }
        return Result.ok("删除成功");
    }

    @Override
    public PageModel getBaseUserList(Map map) {
        Page<Map> mapPage = new Page<>(map);
        IPage iPage = userMapper.selectPage(mapPage, new QueryWrapper<BaseUser>()
                .lambda()
                .like(GeneralUtil.isNotNull(map.get("account")), BaseUser::getAccount, map.get("account"))
                .like(GeneralUtil.isNotNull(map.get("name")), BaseUser::getName, map.get("name")));
        return new PageModel(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public List<Map<String, Object>> getBaseUserAllList() {
        QueryWrapper<BaseUser> wrapper = new QueryWrapper<BaseUser>();
        return userMapper.selectMaps(wrapper);
    }

    @Override
    public BaseUser getBaseUserSingleById(String id) {
        QueryWrapper<BaseUser> wrapper = new QueryWrapper<BaseUser>();
        wrapper.lambda().eq(BaseUser::getUserId, id);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public Result updataPass(BaseUser user) {
        Assert.notNull(user, "用户实体为空!");
        Assert.hasText(user.getUserId(), "用户id不能为空!");
        Assert.hasText(user.getPassword(), "密码不能为空!");
        user.setPassword(MD5.MD5Encode(user.getPassword()));
        int flag = baseMapper.updateById(user);
        if (flag > 0) {
            return Result.ok("密码修改成功!");
        }
        return Result.error("密码修改失败!");
    }
}
